<?php

namespace App\Libs\Strategy;


use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Validation\ValidatesRequests;

abstract class AContext
{
    use ValidatesRequests;
    //当前的数据表对象
    protected $model;

    //具体策略
    protected $strategy;

    //是否开启数据验证
    protected $validate = false;

    //添加前置操作
    protected $beforeCreate = false;

    //更新前置操作
    protected $beforeUpdate = false;

    //删除前置操作
    protected $beforeDelete = false;

    //回调
    protected $callable;

    //验证规则
    protected $rules = [];

    //验证信息
    protected $msg = [];

    //数据
    protected $data;

    //条件
    protected $where;

    public function __construct(Model $model)
    {
        $this->model = $model;
        $this->data = request()->all();
    }

    abstract protected function create(array $data = [],$callable = false);
    abstract protected function update($id,array $data = [],$callable = false);
    abstract protected function delete($id,$callable = false);
    abstract protected function display($page = 10);
    abstract protected function find($id,$callable = false);
    abstract protected function setRules($rule,$msg = []);


    protected function __hook()
    {
        $this->validate === false ?:$this->validates();
        if(is_callable($this->callable))
        {
            $this->data = call_user_func($this->callable,$this->data);
        }
    }

    protected function validates(){
        if(!empty($this->rules) )
        {
            $this->validate(request(),$this->rules,$this->msg);
        }
    }
}
